<template>
  <el-dialog title="任务修改" :visible.sync="showDialog" width="600px" @close="cancelSubmit">
    <el-form ref="formData" :inline="true" :model="formData" label-width="100px" size="small" style="padding-left:70px;">
      <el-form-item label="单位名称:" prop="companyCode">
        <el-select v-model="formData.companyCode" size="small" placeholder="请选择单位" style="width:350px;" @change="companyChange" filter clearable>
          <el-option v-for="item in tree" :label="item.companyName" :value="item.companyCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="场站名称:" prop="stationCode">
        <el-select style="width:350px;" v-model="formData.stationCode" placeholder="请选择场站" clearable filter>
          <el-option v-for="item in stationList" :label="item.stationName" :value="item.stationCode" :key="item.stationCode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="文件描述:"  prop="fileDescribe">
        <el-input
          type="textarea"
          autosize
          placeholder="请输入文件描述"
          style="width:350px;"
          :rows="2"
          v-model="formData.fileDescribe">
        </el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: center;">
      <el-button @click="cancelSubmit" size="small">取 消</el-button>
      <el-button type="primary" @click="submit" size="small">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import stations from "../../../../api/stations"
  import manual from "../../../../api/manual"
  import myTask from "../../../../api/myTask";
  export default {
    name: "dialogModifyTask",
    props: {
      modifyTaskParams: {type: Object, default: () => {}}
    },
    data() {
      return {
        showDialog: false,
        formData: {},
        stationList: [],
        tree: []
      }
    },
    methods: {
      show() {
        this.showDialog = true
        this.init()
      },
      init() {
        this.getCompanyList()
        this.$nextTick(() => {
          this.formData = this.modifyTaskParams
        })
      },
      getCompanyList() {
        stations.getCompanyList().then(res => {
          if (res && res.data.code === 200) {
            this.tree = res.data.data
            this.setCompanyValue()
          }
        }).catch(e => {
          this.$message.error({message: e.message ? e.message : e, center: true})
        })
      },
      setCompanyValue() {
        this.formData.companyCode = this.modifyTaskParams.companyCode
        this.companyChange()
      },
      companyChange() {
        this.getStationByCompany()
      },
      getStationByCompany() {
        manual.getStationByCompany({
          companyCode: this.formData.companyCode
        }).then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.stationList = res.data.data
            this.setStationValue()
          }
        }).catch(e => {
          this.$message.error({message: e.message ? e.message : e, center: true})
        })
      },
      setStationValue() {
        this.formData.stationCode = this.modifyTaskParams.stationCode
      },
      cancelSubmit() {
        this.showDialog = false
      },
      submit() {
        // 调用详情修改接口
        myTask.updateTaskFlowInfo(this.formData).then(res => {
          if (res && res.status === 200 && res.data.code === 200) {
            this.$message.success("修改成功！")
            this.$emit("sendDialogModifyTask")
            this.showDialog = false
          }
        }).catch(e => {
          this.$message.error({message: e.message ? e.message : e, center: true})
        })
      }
    }
  }
</script>

<style scoped>

</style>
